package part1;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

import org.omg.CORBA.portable.OutputStream;

public class Serveur {

	int portNumber = 1025;
	ServerSocket serverSocket = null;
	Socket connectSocket = null;
	String data = "Hello from serverSocket";

	public Serveur(int portNumber) {

		this.portNumber = portNumber;

		// Ouvrir une socket serveur
		try {
			this.serverSocket = new ServerSocket(portNumber);
		} catch (IOException e) {
			System.out.println(e);
		}

		attenteConnexion();
	}

	// Cr�er un objet pour le socket de connexion
	public void attenteConnexion() {
		try {
			System.out.println("Serveur en attente d'une connexion");
			connectSocket = serverSocket.accept();
			read();
		} catch (IOException e) {
			System.out.println(e);
		}

	}

	// Lire des donneees dans la socket de connexion
	public void read() {
		int MAXLENGTH = 256;
		byte[] buff = new byte[MAXLENGTH];
		try {
			InputStream in = connectSocket.getInputStream();
			in.read(buff);
			System.out.println("Message en provenance du Client: " + new String(buff));
		} catch (IOException e) {
			System.out.println(e);
		}

	}

	// Fermer le socket
	public void fermerConnexion() {
		try {
			connectSocket.close(); // Close the socket. We are
			// done with this client!
			serverSocket.close(); // close the serverSocket socket
		} catch (IOException e) {
			System.out.println(e);
		}

	}

}